package com.marktotop.demo;

import javax.swing.*;
import java.sql.*;


/**
 * @author WXLD
 * @data 2021/4/29 - 16:55
 */
public class JDBCService {

    private static final String url = "jdbc:mysql://47.108.95.109:3306/tolerance?serverTimezone=UTC";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(WindowDemo.jFrame,"数据驱动异常，请联系管理员！");
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     */
    public static Connection getConn(){
        try {
            Connection connection = DriverManager.getConnection(url, "root", "123456");
            return connection;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(WindowDemo.jFrame,"数据访问异常，联系管理员！");
            throw new RuntimeException(e);
        }
    }

    public static void release(ResultSet resultSet, Statement statement,Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resultSet = null;
        }

        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement = null;
        }

        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            connection = null;
        }
    }

}
